package com.rock.platform.util;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class DateHelper {
	public static Calendar getCalendar() {
		return getCalendar(new java.util.Date());
	}

	public static Calendar getCalendar(java.util.Date dt) {
		Calendar c = Calendar.getInstance(Locale.CHINA);
		c.setTime(dt);
		c.setFirstDayOfWeek(2);
		c.setMinimalDaysInFirstWeek(7);

		return c;
	}

	public static long getTimeSpanDay(java.util.Date dtBegin,
			java.util.Date dtEnd) {
		long tsDays = dtEnd.getTime() - dtBegin.getTime();
		return (tsDays / 1000L / 60L / 60L / 24L + 1L);
	}

	public static java.util.Date addMonths(java.util.Date dt, int n) {
		Calendar c = getCalendar(dt);
		c.add(2, n);

		return c.getTime();
	}

	public static java.util.Date addDays(java.util.Date dt, int n) {
		return new java.util.Date(dt.getTime() + n * 1000 * 60 * 60 * 24);
	}

	public static int currentYear() {
		Calendar c = getCalendar();
		return c.get(1);
	}

	public static int currentMonth() {
		Calendar c = getCalendar();
		return (c.get(2) + 1);
	}

	public static int currentDay() {
		Calendar c = getCalendar();
		return c.get(5);
	}

	public static int getDateYear(java.util.Date dt) {
		Calendar c = getCalendar(dt);
		return c.get(1);
	}

	public static int getDateMonth(java.util.Date dt) {
		Calendar c = getCalendar(dt);
		return (c.get(2) + 1);
	}

	public static int getDateDay(java.util.Date dt) {
		Calendar c = getCalendar(dt);
		return c.get(5);
	}

	public static int getWeekOfYear(java.util.Date dt) {
		Calendar c = getCalendar(dt);
		return c.get(3);
	}

	public static java.sql.Date getSqlDate(java.util.Date dtDate) {
		return new java.sql.Date(dtDate.getTime());
	}

	public static Timestamp getSqlTimestamp(java.util.Date dtDate) {
		return new Timestamp(dtDate.getTime());
	}

	public static String formatDate(java.util.Date dtDate) {
		return formatDate(dtDate, "yyyy-MM-dd");
	}

	public static String formatDateTime(java.util.Date dtDate) {
		return formatDate(dtDate, "yyyy-MM-dd HH:mm:ss");
	}

	public static String formatDate(java.util.Date dtDate, String strFormat) {
		String strDate = "";
		SimpleDateFormat objFormat = new SimpleDateFormat(strFormat);

		strDate = objFormat.format(dtDate);

		return strDate;
	}

	public static java.util.Date parseDate(String strDate) throws Exception {
		return parseDate(strDate, "yyyy-MM-dd");
	}

	public static java.util.Date parseDateTime(String strDate) throws Exception {
		return parseDate(strDate, "yyyy-MM-dd HH:mm:ss");
	}

	public static java.util.Date parseDate(String strDate, String strFormat)
			throws Exception {
		return new SimpleDateFormat(strFormat).parse(strDate);
	}
}
